---
--类名: StoryManager.lua
--创建日期: 2017-8-23
--创建人: 陈小虎
--功能描述: 
--修改内容:
--修改日期:   
--修改人:      
--修改备注: 
--

local StoryManager = class("StoryManager")

function StoryManager:ctor()
    cc(self):addComponent("components.behavior.EventProtocol"):exportMethods()

end

function StoryManager:clear()
	self:setStoryTaskId(nil)
	self:setEnterStoryMode(false)
	self:setExitStoryModeDelay(false)
end

function StoryManager:setBaseNode(baseNode)
	self._baseNode = baseNode
end

function StoryManager:getBaseNode()
	return self._baseNode
end

function StoryManager:getStoryMode()
	return self._storyModeView
end

function StoryManager:removeStoryMode()
	if not self:getStoryMode() then return end
    self:getBaseNode():removeChild(self:getStoryMode())
    self._storyModeView = nil
end

function StoryManager:setStoryTaskId(taskId)
    self._storyTaskId = taskId
end

function StoryManager:getStoryTaskId()
    return self._storyTaskId
end

function StoryManager:setEnterStoryMode(enter)
    self._enterStory = enter
end

function StoryManager:isEnterStoryMode()
    return self._enterStory
end

function StoryManager:setExitStoryModeDelay(isDelay)
    self._exitStoryModeDelay = isDelay
end

function StoryManager:isExitStoryModeDelay()
    return self._exitStoryModeDelay
end

function StoryManager:isStopStoryEvent()
    if self:isEnterStoryMode() 
        or self:isExitStoryModeDelay() then
        return true
    end
    return false
end

function StoryManager:getStoryBlackMaskHeight()
    return self:getStoryMode() and self:getStoryMode():getMaskHeight() or 0
end

function StoryManager:stopStoryDelayAction()
    if self._storyModeDelayAction then
        self:getBaseNode():stopAction(self._storyModeDelayAction)
        self._storyModeDelayAction = nil
    end
end

function StoryManager:showMask(callback,time,hideBorder)
    if self:getStoryMode() then
        self:getStoryMode():showMask(callback,time,hideBorder)
    end
end

function StoryManager:enterStoryMode(npcId,time,hideBorder,event,callback)
    self:stopStoryDelayAction()
    self:setEnterStoryMode(true)

    -- app:dispatchCustomEvent("EVENT_CLOSE_TRACK_LIST")
    UIHandler:removeAllUI(true)
    DoEventHandler.clearGatherStatus()
    FieldManager:breakTransferForStory()
    FieldManager:clearAutoWayFindingParams()
    FieldManager:onGetOffRide()
    FieldManager:hideAllIMBubble()
    FieldManager:stopShowAllIMBubble(true)
    FieldManager:hideTaskFollowMercenarys()
    FieldManager:stopTouch(true)
    FieldManager:startHideCharacters(true)

    -- FieldManager:stopShowMyCharacterUpHead(true)
    -- FieldManager:stopShowAllCharactersUpHead(true)
    FieldManager:updateAllCharactersUpHead(false)
    if not self:getStoryMode() then
        local StoryModeView = app:getClass("StoryModeView")
        self._storyModeView = StoryModeView.new()
        self._storyModeView:setName("StoryModeView")
        self:getBaseNode():addChild(self._storyModeView,SceneZOrder.storyModeZOrder)
    end
    
    local taskId = event.mTaskId
    self:setStoryTaskId(tonumber(taskId))
   
    NodeFactory:arrowAnimationViewing(false)
    
    NpcManager:hideNpcByConfigInStory()
    NpcManager:stopUpdateNpcPlayEvents(true)
    NpcManager:stopAllNpcUpHeadShow(true)
    MapManager:fadeMapNode({time = 0.5,back = false})
    NpcManager:allNpcUpHeadNodeVisibility(false)
--    NpcManager:disasterNpcVisibility(false)
    NpcManager:oreNpcVisibility(false)
    MapManager:stopCameraZoomForStory(true)

    self:getStoryMode():enter(function()
        if callback then
            callback()
        end
    end,npcId,time,hideBorder)

    
    DungeonManager:hideAllBNpcs()

    self.mapScale = MapManager:getMapScale()
end

function StoryManager:hideStoryModeViewMask()
    if not self:getStoryMode() then return end
    self:getStoryMode():exit(nil, nil, -1)
end 

function StoryManager:clearStoryStatus(event)
    self:removeStoryMode()

    self:clearAllStatus(true)

    if not DoEventHandler.isDoingEvents() then
        app:dispatchCustomEvent("EVENT_TASK_FINISH_STORY")
        FieldManager:getWeatherSystem():restartWeather()
    end

    local seqAction = cc.Sequence:create(
        cc.DelayTime:create(1),
        cc.CallFunc:create(function()
        UIHandler:enableMainTaskTrackingTouch()
    end))
    self:getBaseNode():runAction(seqAction)
    if DoEventHandler.isFrontEvents() then
        TaskManager:C2S_TaskTalkRequest(self:getStoryTaskId())
    else
        TaskManager:storyGetTaskReward(self:getStoryTaskId())
    end

    MapManager:getMap():setZoomScale(self.mapScale)
end

function StoryManager:exitStoryMode(npcId,time,exitCall)
    if not self:getStoryMode() then return end
    UIHandler:disableMainTaskTrackingTouch()
    self:getStoryMode():exit(function()
        if exitCall then
            exitCall()
        end
    end,npcId,time)
end

-- 强制结束当前剧情（回退到之前的，不完成剧情）
function StoryManager:forceExitCurStory()
    if not self:getStoryMode() then return end
    if not self:isEnterStoryMode() then return end

    self:getStoryMode():exit(nil, nil, 0)

    self:removeStoryMode()
    self:clearAllStatus(true)
    MapManager:getMap():setZoomScale(self.mapScale)

    DoEventHandler.stopNodeActions()
    UIHandler:removeBlackScreenMask()
end

function StoryManager:pauseStoryModeMask()
    if self:getStoryMode() then
        self:getStoryMode():pauseMask()
    end
end

function StoryManager:resumeStoryModeMask()
    if self:getStoryMode() then
        self:getStoryMode():resumeMask()
    end
end

function StoryManager:clearTransferStatus()
    self:clearAllStatus()
    UIHandler:showDesktop()
end

function StoryManager:clearAllStatus(isStory)

    if not self:getStoryMode() then 
        if self._storyModeDelayAction then
            self:stopStoryDelayAction()
        end
    else
        self:getStoryMode():exit(nil,nil,0)
        self:removeStoryMode()
    end

    self:setEnterStoryMode(false)

    BattleManager:setBattle(false)
    BattleManager:waitBattle(false)

    DungeonManager:showAllBNpcs()
    TeamManager:resetTeamMemberPositionToLeaderForStory()
    NodeFactory:arrowAnimationViewing(true,self:getStoryTaskId())
    
    UIHandler:removeAllUI()
    UIHandler:clearTouchMask()
    UIHandler:hideGatherControl()
    UIHandler:blackScreenFade("fadeOut",0)
    UIHandler:enableMainTaskTrackingTouch()
    UIHandler:getStoryModeUi():closeTouchMask()
    UIHandler:handleMessage({id = Constant.FUNC_TYPE_TRANSFER,mode = "close"})
    UIHandler:showDesktop()

    FieldManager:setTransferring(false)
    FieldManager:setTransferStart(false)
    FieldManager:stopTouch(false)
    FieldManager:hideAllBubble()
    FieldManager:updateAllCharactersUpHead(true)
    FieldManager:stopShowAllIMBubble(false)
    FieldManager:stopHideCharacters(true)
    FieldManager:forceShowMainCharacter()
    FieldManager:exceptSelfCharactersVisibility(true)
    FieldManager:stopTouch(false)
    FieldManager:autoGetOnRide()
    FieldManager:deletePuppetCharacter()
    FieldManager:showTaskFollowMercenarys()
    FieldManager:followMercenaryVisibility(true)

    DoEventHandler.setCurrrentMark(nil)
    DoEventHandler.removeSkipConfirm()
    DoEventHandler.stopAllAudio()
    DoEventHandler.clearAllAudioIds()
    DoEventHandler.clearEventsStatus()
--    DoEventHandler.gatherOver()
    DoEventHandler.setTracking(false)
    DoEventHandler.setDoingEvents(false)
    DoEventHandler.setSkipEvents(false)
    DoEventHandler.removeAllAni()
    DoEventHandler.removeAllShowNpc()
    -- DoEventHandler.clearAllShowNpcIds()
    DoEventHandler.revertAllNpcVisible()
    DoEventHandler.removeSkipStoryView()
    DoEventHandler.DoTaskBattleEvent = false
    DoEventHandler.DoDisasterBattle = false

    MapManager:revertChangeMapStatus()
    MapManager:pauseRunCameraZoomForEvent(false)
    MapManager:pauseEventAutoAdjustCamera(false) 
    MapManager:stopCameraZoomForStory(false)
    MapManager:pauseAdjustCamera(false)
    MapManager:fadeMapNode({time = 0.5,back = true})
    MapManager:setFocusCharacterForInit({character = FieldManager:getMyCharacter()})
--    MapManager:backToSaveMapLayerScale({time = 0})

    NpcManager:showNpcByConfigOutStory()
    NpcManager:showAllNpcMark()
    NpcManager:stopUpdateNpcPlayEvents(false)
    NpcManager:stopAllNpcUpHeadShow(false)
    NpcManager:allNpcUpHeadNodeVisibility(true)
--    NpcManager:disasterNpcVisibility(true)
    NpcManager:oreNpcVisibility(true)
    NpcManager:allNpcAbslutelyVisibility(true)

    if not isStory then
        NpcManager:clearAllMapNpcDoing()
        NpcManager:resetAllNpcEventStatus()
        NpcManager:clearAreaDoingStatus()
    end

    NpcManager:fadeInNpcs()
    NpcManager:resetNpcMask()
    MapManager:resetMapMask()
end

return StoryManager